//
//  MainViewBar.m
//  MyShiDiQuan
//
//  Created by cvicseks-mac1 on 16/9/7.
//  Copyright © 2016年 cvicseks-mac1. All rights reserved.
//

#import "MainViewBar.h"
#import "UIButton+ImagePosition.h"

@interface MainViewBar()
@property (nonatomic, copy) NSArray *images;
@property (nonatomic, copy) NSArray *titles;
@end

@implementation MainViewBar

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

- (void)setBtnsWithTitle:(NSArray *)titles andImage:(NSArray *)images
{
    _titles = titles;
    _images = images;
}

- (void)btnClicked:(UIButton *)sender{
    if (self.btnClickedBlock) {
        __block UIButton *btn = sender;
        self.btnClickedBlock(btn);
    }
}

- (void)addBtnClickedActionBlock:(void (^)(btnTag))btnClickedBlock{
    self.btnClickedBlock = ^(UIButton *btn){
        if (btnClickedBlock) {
            btnTag tag = btn.tag;
            btnClickedBlock(tag);
        }
    };
}

- (void)layoutSubviews{
    // padding 5
    CGFloat width = self.frame.size.width-10;
    CGFloat height = self.frame.size.height-10;
    
    CGFloat btnwidth = width/(self.titles.count);
    for (int i = 0; i<self.titles.count; i++) {
        UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(i*btnwidth+5, 5, btnwidth, height)];
        btn.tag = i;
        [btn setImage:[UIImage imageNamed:self.images[i]] forState:UIControlStateNormal];
        [btn setTitle:self.titles[i] forState:UIControlStateNormal];
        [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [btn setImagePosition:ImagePositionTop spacing:0];
        [btn addTarget:self action:@selector(btnClicked:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:btn];
    }

}

@end
